// Tile.cpp: implementation of the CTile class.
//
//////////////////////////////////////////////////////////////////////

#include "Tile.h"
#include <stdio.h>
#include <GL/glaux.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTile::CTile()
{

}

CTile::~CTile()
{
	delete [] texture;
}

////////////////////////////////////////////////////////////////////////////////////////
// Load a Bitmap file
////////////////////////////////////////////////////////////////////////////////////////
AUX_RGBImageRec* CTile::Loadbmp(char *Filename)				// Loads A Bitmap Image
{
	FILE *File=NULL;									// File Handle

	if (!Filename)										// Make Sure A Filename Was Given
	{
		return NULL;									// If Not Return NULL
	}

	File=fopen(Filename,"r");							// Check To See If The File Exists

	if (File)											// Does The File Exist?
	{
		fclose(File);									// Close The Handle
		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer
	}

	return NULL;										// If Load Failed Return NULL
}


int CTile::LoadGLTextures(int numtexture)              		// Load Bitmaps And Convert To Textures
{
	int Status=FALSE;									// Status Indicator

	AUX_RGBImageRec* TextureImage=NULL;		// Create Storage Space For The Texture		
	//memset(TextureImage,0,sizeof(void *)*1);           	// Set The Pointer To NULL
	//memset(TextureImage,0,sizeof(void *)*1);           	// Set The Pointer To NULL

	texture = new GLuint[numtexture];
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);
	glGenTextures(numtexture, &texture[0] );					// Create numtexures Textures

	for(int i=0; i <= numtexture; i++)
	{
		char texname[20];
		itoa(i,texname,10);
		strcat(texname,".bmp");		
		TextureImage = Loadbmp(texname);		

		TileWidth = TextureImage->sizeX;
		TileHeight = TextureImage->sizeY;

		// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
		Status=TRUE;									// Set The Status To TRUE		

		// Create Nearest Filtered Texture
		glBindTexture(GL_TEXTURE_2D, texture[i]);
		//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
	}


	if (TextureImage)
	{
		if (TextureImage->data)
		{
			free(TextureImage->data);
		}
	    free(TextureImage);
	}	

	return Status;										// Return The Status
}

void CTile::DrawIsometricTile(int xPos, int yPos, int TileID )
{
	// tile now holds the ID# of the tile at location map[y][x].
	glBindTexture(GL_TEXTURE_2D, TileID+1);
    
	/////////////////////////////////////////////////////////////////////////////
	// Formula 
	// int x = ( xPos - xTileDeb - 1) * ( TileWidth ) + ( yPos & 1) * (TileWidth/2); 
	// int y = ( yPos - yTileDeb )    * ( TileWidth / 4 );
	/////////////////////////////////////////////////////////////////////////////
	int x = xPos;
	int y = yPos;
    glBegin(GL_TRIANGLE_STRIP );
	    glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x), float(y), 0.0f);
	    glTexCoord2f(1.0f, 0.0f); glVertex3f(float(x + (TileWidth >> 1)), float(y - ( TileWidth >> 2 )), 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(float(x + (TileWidth >> 1)), float(y + ( TileWidth >> 2 )), 0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(float(x + TileWidth), float(y), 0.0f);
	glEnd();	
}

void CTile::DrawTile(int xPos, int yPos, int TileID )
{
	// tile now holds the ID# of the tile at location map[y][x].
	glBindTexture(GL_TEXTURE_2D, texture[TileID+1]);
							
	// Here we bind the texture that we need, either texture[0] or texture[1], based on the value of tile, which holds the current tile's ID#.
    glBegin(GL_QUADS);
	    glTexCoord2f(0.0f, 0.0f); glVertex3f(float(xPos), float(yPos), 0.0f);
	    glTexCoord2f(1.0f, 0.0f); glVertex3f(float(xPos + 1), float(yPos), 0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(float(xPos + 1), float(yPos + 1), 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(float(xPos), float(yPos + 1), 0.0f);
	glEnd();
}
